<?php
	define('BDD_DSN', 'mysql:dbname=cdg;host=localhost'); // DSN BDD
	define('BDD_UTILISATEUR', 'root'); // LOGIN BDD
	define('BDD_MOT_DE_PASSE', ''); // MOT DE PASSE BDD
	define('BDD_HOST', 'localhost'); // SERVEUR BDD
	define('BDD_BASE', 'cdg'); // BASE BDD
	define('DIR_LIB', 'lib/');
	define('DIR_UPLOAD', 'xls/');
	define('DIR_LOGS', 'logs/');
	define('DIR_CACHE', 'cache/');
	define('DIR_TMP', 'tmp/');
	define('DIR_TRAD', 'trad/');
	define('DIR_IMG', 'img/');
	
	include_once('../../lib/class.fonctions.php');
	include_once('../../lib/class.cache.php');
	include_once('../../lib/class.db.php');
	include_once('../../lib/class.debug.php');
	include_once('../../lib/class.activerecord.php');
	include_once('../../lib/class.secure.php');
	
	// On instancie les classes principales
	$gp_fonc = GP_fonctions::getInstance();
	$gp_db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
	$gp_request = GP_secure::getInstance();
	
	
	// Mise en place de l'ActiveRecord
	GP_ActiveRecord::setDefaultDBConnection($gp_db);
	
	// on accroit les perf si possible
	$version = explode('.', PHP_VERSION, 2);
	if($version[0] * 10 + $version[1] < 53)
		set_magic_quotes_runtime(0);
	
	@set_time_limit(0);
	
	$limite_memoire = @ini_get('memory_limit');
	
	if (!empty($limite_memoire))
	{
		$unite = strtolower(substr($limite_memoire, -1, 1));
		$limite_memoire = (int) $limite_memoire;

		if ($unite == 'k')
			$limite_memoire = floor($limite_memoire / 1024);
		else if ($unite == 'g')
			$limite_memoire *= 1024;
		else if (is_numeric($unite))
			$limite_memoire = floor((int) ($limite_memoire . $unite) / 1048576);
		$limite_memoire = max(128, $limite_memoire) . 'M';
	}
	else
		$limite_memoire = '128M';
	@ini_set('memory_limit', $limite_memoire);
	
	$tplset = 'function set##uchamp##($val)
		{
			$this -> ##champ## = $val;
		}
		
		function get##uchamp##()
		{
			return $this -> ##champ##;
		}';
	
	$q = "SHOW TABLES";
	$res = $gp_db -> fetchArray($q);
	$alltables = array();
	$tpl = file_get_contents('tpl/objet.tpl');
	foreach ($res as $tablet => $tab)
	{
		foreach ($tab as $c => $table)
		{
			$obj = str_replace('##table##', $table, $tpl);
			$set = '';
			$var = '';
			$q2 = "DESCRIBE " . $table;
			$res2 = $gp_db -> fetchArray($q2);
			foreach ($res2 as $f => $tab2)
			{
				foreach ($tab2 as $type => $champ)
				{
					if ($type == 'Field')
					{
						$uchamp = ucfirst(strtolower($champ));
						$uchamp = str_replace('_', '', $uchamp);
						$this_set = str_replace('##uchamp##', $uchamp, $tplset);
						$this_set = str_replace('##champ##', $champ, $this_set);
						$set .= $this_set . "\n\n\t\t";
						$var .= 'public $' . $champ . ";\n\t\t";
					}
				}
			}
			$set = substr($set, 0, -4);
			$var = substr($var, 0, -3);
			$obj = str_replace('##set##', $set, $obj);
			$obj = str_replace('##var##', $var, $obj);
			$fic = 'objet/'.$table.'.php';
			@unlink($fic);
			file_put_contents($fic, $obj);
		}
	}
	die('objets crees');
?>